package itsm.isperp.module.controller.print;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.print.WordTemplate;
import itsm.isperp.framework.print.XwpUtils;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.response.ResultMessage;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 分类管理控制器
 * 
 * @author lizx
 * @date 2014-7-17
 */
@Controller
@RequestMapping("print/print")
public class PrintController extends BaseController{
	
	@RequestMapping(value = { "downloadView" })
	public ModelAndView downloadView(HttpServletRequest req){
		return this.getModelAndView();
	}

	@Override
	public BaseService getService() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getPageTitle() {
		// TODO Auto-generated method stub
		return null;
	}
	

  @RequestMapping(value = { "printReport1" })
  @ResponseBody
  public ResultMessage printReport1(HttpServletRequest req,
      HttpServletResponse response, RedirectAttributes redirectAttributes,String name,Date time,String headOfUnit,
      String telOfUnit,String projManager,String telOfProj,String officer,String telOfOfficer) {
    String path=ContextHolder.getApplicationPath();
    String sourcePath=path+"uploadFile\\templateDoc\\封面.docx";
    String outPath=path+"uploadFile\\temp\\model.docx";
    XwpUtils xwpUtils=new XwpUtils();
    File file = new File(sourcePath);
    InputStream is=null;
    XWPFDocument doc=null;
    Map<String, Object> map = new HashMap<String, Object>();
    try {
       is = new FileInputStream(sourcePath);
       doc = new XWPFDocument(is);
    } catch (IOException exception) {
      exception.printStackTrace();
    }
    SimpleDateFormat sft = new SimpleDateFormat("yyyy年MM月");
    String date = sft.format(time);
    
    map.put("name", name);
    map.put("time", date);
    map.put("headOfUnit", headOfUnit);
    map.put("telOfUnit", telOfUnit);
    map.put("projManager", projManager);
    map.put("telOfProj", telOfProj);
    map.put("officer", officer);
    map.put("telOfOfficer", telOfOfficer);
    
 // 替换段落里面的变量
    xwpUtils.replaceInPara(doc, map);
    // 替换表格里面的变量
    xwpUtils.replaceInTable(doc, map);
    File file1 = new File(outPath);
    FileOutputStream out;
    try {
      out = new FileOutputStream(file1);
      BufferedOutputStream bos = new BufferedOutputStream(out);
      doc.write(bos);
      xwpUtils.close(is);
      xwpUtils.close(bos);
      String filename="cover.docx";
      
   // 以流的形式下载文件。
      InputStream fis = new BufferedInputStream(new FileInputStream(outPath));
      byte[] buffer = new byte[fis.available()];
      fis.read(buffer);
      fis.close();
      // 清空response
      response.reset();
      // 设置response的Header
      response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
      response.addHeader("Content-Length", "" + file.length());
      OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
      response.setContentType("application/octet-stream");
      toClient.write(buffer);
      toClient.flush();
      toClient.close();

      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return ResultMessage.success();
  }
  
  @RequestMapping(value = { "printReport2" })
  @ResponseBody
  public String printReport2(HttpServletRequest req,
      HttpServletResponse response, RedirectAttributes redirectAttributes,String companyName,
      String year,String season,String eqpTimes,String prtTimes,String mtTimes,String telTimes,
      String comTimes,String routerTimes,String other,String processed,String pending,String keyOfWork,String schedule,String suggestion,
      String pTime1,String pKey1,String pCompleteTime1,String pStandard1,String pManage1,String pCustodian1,String pRemark1,
      String pTime2,String pKey2,String pCompleteTime2,String pStandard2,String pManage2,String pCustodian2,String pRemark2,
      String pTime3,String pKey3,String pCompleteTime3,String pStandard3,String pManage3,String pCustodian3,String pRemark3,
      String pTime4,String pKey4,String pCompleteTime4,String pStandard4,String pManage4,String pCustodian4,String pRemark4,
      String opTime1,String opKey1,String opCompleteTime1,String opStandard1,String opManage1,String opCustodian1,String opRemark1,
      String opTime2,String opKey2,String opCompleteTime2,String opStandard2,String opManage2,String opCustodian2,String opRemark2,
      String opTime3,String opKey3,String opCompleteTime3,String opStandard3,String opManage3,String opCustodian3,String opRemark3,
      String opTime4,String opKey4,String opCompleteTime4,String opStandard4,String opManage4,String opCustodian4,String opRemark4
      ) {
    String path=ContextHolder.getApplicationPath();
    String sourcePath=path+"uploadFile\\templateDoc\\报表.docx";
    String outPath=path+"uploadFile\\temp\\report.docx";
    
    XwpUtils xwpUtils=new XwpUtils();
    File file = new File(sourcePath);
    InputStream is=null;
    XWPFDocument doc=null;
    Map<String, Object> map = new HashMap<String, Object>();
    try {
       is = new FileInputStream(sourcePath);
       doc = new XWPFDocument(is);
    } catch (IOException exception) {
      exception.printStackTrace();
    }
    
    map.put("companyName", companyName);
    map.put("year", year);
    map.put("season", season);
    map.put("eqpTimes", eqpTimes);
    map.put("prtTimes", prtTimes);
    map.put("mtTimes", mtTimes);
    map.put("telTimes", telTimes);
    map.put("comTimes", comTimes);
    map.put("routerTimes", routerTimes);
    map.put("other", other);
    map.put("processed", processed);
    map.put("pending", pending);
    map.put("keyOfWork", keyOfWork.replace("<br>", "\n"));
    map.put("schedule", schedule.replace("<br>", "\n"));
    map.put("suggestion", suggestion.replace("<br>", "\n"));
    
    //下季度工作计划
    map.put("pTime1", pTime1);
    map.put("pKey1", pKey1);
    map.put("pCompleteTime1", pCompleteTime1);
    map.put("pStandard1", pStandard1);
    map.put("pManage1", pManage1);
    map.put("pCustodian1", pCustodian1);
    map.put("pRemark1", pRemark1);
    
    map.put("pTime2", pTime2);
    map.put("pKey2", pKey2);
    map.put("pCompleteTime2", pCompleteTime2);
    map.put("pStandard2", pStandard2);
    map.put("pManage2", pManage2);
    map.put("pCustodian2", pCustodian2);
    map.put("pRemark2", pRemark2);
    
    map.put("pTime3", pTime3);
    map.put("pKey3", pKey3);
    map.put("pCompleteTime3", pCompleteTime3);
    map.put("pStandard3", pStandard3);
    map.put("pManage3", pManage3);
    map.put("pCustodian3", pCustodian3);
    map.put("pRemark3", pRemark3);
    
    map.put("pTime4", pTime4);
    map.put("pKey4", pKey4);
    map.put("pCompleteTime4", pCompleteTime4);
    map.put("pStandard4", pStandard4);
    map.put("pManage4", pManage4);
    map.put("pCustodian4", pCustodian4);
    map.put("pRemark4", pRemark4);
    
    //其他工作
    map.put("opTime1", opTime1);
    map.put("opKey1", opKey1);
    map.put("opCompleteTime1", opCompleteTime1);
    map.put("opStandard1", opStandard1);
    map.put("opManage1", opManage1);
    map.put("opCustodian1", opCustodian1);
    map.put("opRemark1", opRemark1);
    
    map.put("opTime2", opTime2);
    map.put("opKey2", opKey2);
    map.put("opCompleteTime2", opCompleteTime2);
    map.put("opStandard2", opStandard2);
    map.put("opManage2", opManage2);
    map.put("opCustodian2", opCustodian2);
    map.put("opRemark2", opRemark2);
    
    map.put("opTime3", opTime3);
    map.put("opKey3", opKey3);
    map.put("opCompleteTime3", opCompleteTime3);
    map.put("opStandard3", opStandard3);
    map.put("opManage3", opManage3);
    map.put("opCustodian3", opCustodian3);
    map.put("opRemark3", opRemark3);
    
    map.put("opTime4", opTime4);
    map.put("opKey4", opKey4);
    map.put("opCompleteTime4", opCompleteTime4);
    map.put("opStandard4", opStandard4);
    map.put("opManage4", opManage4);
    map.put("opCustodian4", opCustodian4);
    map.put("opRemark4", opRemark4);
    
 // 替换段落里面的变量
    xwpUtils.replaceInPara(doc, map);
    // 替换表格里面的变量
    xwpUtils.replaceInTable(doc, map);
    File file1 = new File(outPath);
    FileOutputStream out;
    try {
      out = new FileOutputStream(file1);
      BufferedOutputStream bos = new BufferedOutputStream(out);
      doc.write(bos);
      xwpUtils.close(is);
      xwpUtils.close(bos);
      String filename="report.docx";
      
   // 以流的形式下载文件。
      InputStream fis = new BufferedInputStream(new FileInputStream(outPath));
      byte[] buffer = new byte[fis.available()];
      fis.read(buffer);
      fis.close();
      // 清空response
      response.reset();
      // 设置response的Header
      response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
      response.addHeader("Content-Length", "" + file.length());
      OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
      response.setContentType("application/octet-stream");
      toClient.write(buffer);
      toClient.flush();
      toClient.close();

      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return "SUCCESS";
  }
}
